package com.person.monitor;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;


import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

import static android.app.PendingIntent.FLAG_IMMUTABLE;
import static android.content.Context.NOTIFICATION_SERVICE;

public class NotifyUtil {

    public static String MESSAGE_CHANNEL_ID = "CHANNEL_ID_01";
    public static String MESSAGE_CHANNEL_NAME = "CHANNEL_ID_NAME";

    public final  static String NOTIFY_EXTRA = "notify_extra";

    private String TAG = NotifyUtil.class.getSimpleName();
    private NotificationManager notificationManager;
    private Context context;
    private static NotifyUtil notifyUtil;

    private NotifyUtil(Context c){
        context = c;
        if(notificationManager == null) {
            notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
            createNotificationChannel();
        }
    }

    public static synchronized NotifyUtil getInstance(Context c){
        if(notifyUtil == null){
            notifyUtil = new NotifyUtil(c);
        }
        return notifyUtil;
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(MESSAGE_CHANNEL_ID, MESSAGE_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableLights(false); //是否在桌面icon右上角展示小红点
            channel.setLightColor(Color.GREEN); //小红点颜色
            channel.setShowBadge(false); //是否在久按桌面图标时显示此渠道的通知
            channel.enableVibration(true);
            channel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, Notification.AUDIO_ATTRIBUTES_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }
    }

    public void showNotify(String title,String content){
        showNotify(context, title, content, "cc.sachsen.bgim.user.mvp.activity.SplashActivity",null);
    }

    public void showNotify(Context context, String title, String content, String className, Bundle extra) {
        notificationManager.notify(1, getNotify(context, title, content, className, extra));
    }

    public Notification getNotify(Context context, String title, String text, String className , Bundle extra){
        Intent intent = new Intent();
        intent.setClassName(context.getPackageName(),className);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        if(extra != null) {
            intent.putExtra(NOTIFY_EXTRA,extra);
        }

        MyLog.D(TAG,"getNotify className="+className);

        PendingIntent piResult0 = PendingIntent.getActivity(context, 0, intent, FLAG_IMMUTABLE);

        Notification.Builder builder = new Notification.Builder(context);
        builder.setContentTitle(title);
        builder.setContentText(text);
        builder.setAutoCancel(true);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));
        builder.setContentIntent(piResult0);
        builder.setDefaults(Notification.DEFAULT_SOUND);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(MESSAGE_CHANNEL_ID);
        }
        Notification notification = builder.build();
        try {
            Field field = notification.getClass().getDeclaredField("extraNotification");
            Object extraNotification = field.get(notification);
            Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);
            method.invoke(extraNotification, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return notification;
    }


    public String getLauncherClassName(Context mainContext,String packageName) {
        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resolveIntent.setPackage(packageName);
        PackageManager pManager = mainContext.getApplicationContext().getPackageManager();
        List<ResolveInfo> apps = pManager.queryIntentActivities(resolveIntent,0);

        ResolveInfo ri = apps.iterator().next();
        if (ri != null) {
            String startappName = ri.activityInfo.packageName;
            String className = ri.activityInfo.name;

            return className;
        }
        return null;
    }

    public void cancelAll(){
        if(notificationManager != null){
            notificationManager.cancelAll();
        }
    }
}
